﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Server.Data;
using Server.Model;

namespace Server.Biz {
    public class UserService : BaseService
    {
        UserDAO dao = null;
        public UserService()
        {
            dao = new UserDAO();
        }

        public void CreateUser(ParentInfo pParent, long? pUserID, string pUserName)
        {
            long parentID = CreateParent(pParent, pUserID, pUserName);
            foreach (ClientInfo client in pParent.Clients)
            {
                this.CreateClient(client, parentID, pParent.UserName);
            }
        }

        /// <summary>
        /// 但是现在有一个隐忧：如果以后Client的数目发生变化的话，这里不得不进行新的维护
        /// </summary>
        /// <param name="pParent"></param>
        /// <param name="pUserID"></param>
        /// <param name="pUserName"></param>
        public void UpdateUser(ParentInfo pParent, long pUserID, string pUserName)
        {
            this.UpdateParent(pParent, pUserID, pUserName);
            foreach (ClientInfo client in pParent.Clients)
            {
                this.UpdateClient(client, pUserID, pUserName);
            }
        }

        private long CreateParent(ParentInfo pParent, long? pUserID, string pUserName)
        {
            return dao.CreateParent(pParent, pUserID, pUserName);
        }

        private void UpdateParent(ParentInfo pParent, long pUserID, string pUserName)
        {
            dao.UpdateParent(pParent, pUserID, pUserName);
        }

        private void CreateClient(ClientInfo pClient, long pUserID, string pUserName)
        {
            dao.CreateClient(pClient, pUserID, pUserName);
        }

        private void UpdateClient(ClientInfo pClient, long pUserID, string pUserName)
        {
            dao.UpdateClient(pClient, pUserID, pUserName);
        }
    }
}
